//
//  MLWNetTool.m
//  KITCHEN_STORIES
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 MLW. All rights reserved.
//

#import "MLWNetTool.h"
#import <AFNetworking.h>

@implementation MLWNetTool

+ (void)GetNetWithUrl:(NSString *)url
                 body:(id)body
               header:(NSDictionary *)headers
             response:(MLWResponseStyle)response
              success:(blockOfSuccess)success
              failure:(blockOfFailure)failure

{
    //1.获取网络管理者:
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //2.请求头的设置:
    if (headers) {
        for (NSString *key in headers) {
            [manager.requestSerializer setValue:headers[key] forHTTPHeaderField:key];
        }
    }
    
  
    //3.设置返回数据的类型:
    switch (response) {
        case MLWJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case MLWDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer
                                          serializer];
            break;
        case MLWXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
            
        default:
            break;
    }
    
    //4.设置数据响应类型:
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    
    //5.iOS9 -- UTF-8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    //6.发送请求
    [manager GET:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //回调:
        success(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
        NSLog(@"%@", error);
    }];


}

+ (void)PostNetWithUrl:(NSString *)url
                  body:(id)body
             bodyStyle:(MLWRequdstStyle)bodyStyle
                header:(NSDictionary *)headers
              response:(MLWResponseStyle)response
               success:(blockOfSuccess)success
               failure:(blockOfFailure)failure
{
    
    
    //1.获取网络管理者:
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //2.设置body数据类型:
    switch (bodyStyle) {
        case MLWBodyJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case MLWBodyString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable * _Nullable error) {
                return parameters;
            }];
            break;
        default:
            break;
    }
    
    
    //3.请求头的设置:
    if (headers) {
        for (NSString *key in headers.allKeys) {
            [manager.requestSerializer setValue:headers[key] forHTTPHeaderField:key];
        }
    }
    //4.设置返回数据的类型:
    switch (response) {
        case MLWJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case MLWDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer
                                          serializer];
            break;
        case MLWXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
            
        default:
            break;
    }
    
    
    //5.设置数据响应类型:
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    
    //6.iOS9 -- UTF-8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //7.发送请求
    [manager POST:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //回调:
        success(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
        NSLog(@"%@", error);
    }];
    
    
    
    
    
}






@end
